﻿package org.xhs.report.common.ui.toolbar
{
    import baidu.ui.containers.*;
    import baidu.ui.core.*;
    import baidu.ui.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class AutoLayoutContainer extends ContainerBase
    {
        protected var _childsLayoutInfo:Dictionary;
        protected var _dispatcher:EventDispatcher;

        public function AutoLayoutContainer()
        {
            this._childsLayoutInfo = new Dictionary(true);
            this._dispatcher = new EventDispatcher();
            this._dispatcher.addEventListener(Event.CHANGE, this.childPropertyChangeHandler);
            return;
        }// end function

        protected function childPropertyChangeHandler(event:Event) : void
        {
            invalidate(Invalidation.LAYOUT);
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            super.width = param1;
            invalidate(Invalidation.SIZE);
            this.drawNow();
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            super.height = param1;
            invalidate(Invalidation.SIZE);
            this.drawNow();
            return;
        }// end function

        override public function get classStyles() : Object
        {
            return mergeStyles(super.classStyles, defaultStyles);
        }// end function

        override public function addChild(param1:DisplayObject) : DisplayObject
        {
            super.addChild(param1);
            if (!this._childsLayoutInfo[param1])
            {
                this._childsLayoutInfo[param1] = new LayoutInfo(this._dispatcher);
            }
            param1.addEventListener(BUIEvent.SIZE_CHANGE, this.childResizeHandler, false, 0, true);
            invalidate(Invalidation.LAYOUT);
            return param1;
        }// end function

        override public function addChildAt(param1:DisplayObject, param2:int) : DisplayObject
        {
            super.addChildAt(param1, param2);
            if (!this._childsLayoutInfo[param1])
            {
                this._childsLayoutInfo[param1] = new LayoutInfo(this._dispatcher);
            }
            param1.addEventListener(BUIEvent.SIZE_CHANGE, this.childResizeHandler, false, 0, true);
            invalidate(Invalidation.LAYOUT);
            return param1;
        }// end function

        override public function removeChild(param1:DisplayObject) : DisplayObject
        {
            super.removeChild(param1);
            delete this._childsLayoutInfo[param1];
            param1.removeEventListener(BUIEvent.SIZE_CHANGE, this.childResizeHandler);
            invalidate(Invalidation.LAYOUT);
            return param1;
        }// end function

        override public function removeChildAt(param1:int) : DisplayObject
        {
            var _loc_2:* = super.removeChildAt(param1);
            delete this._childsLayoutInfo[_loc_2];
            _loc_2.removeEventListener(BUIEvent.SIZE_CHANGE, this.childResizeHandler);
            invalidate(Invalidation.LAYOUT);
            return _loc_2;
        }// end function

        override public function swapChildren(param1:DisplayObject, param2:DisplayObject) : void
        {
            invalidate(Invalidation.LAYOUT);
            super.swapChildren(param1, param2);
            return;
        }// end function

        override public function swapChildrenAt(param1:int, param2:int) : void
        {
            invalidate(Invalidation.LAYOUT);
            super.swapChildrenAt(param1, param2);
            return;
        }// end function

        protected function childResizeHandler(event:BUIEvent) : void
        {
            invalidate(Invalidation.LAYOUT);
            return;
        }// end function

        override protected function draw() : void
        {
            if (isInvalid(Invalidation.LAYOUT, Invalidation.STYLES, Invalidation.SIZE))
            {
                this.drawLayout();
                dispatchEvent(new BUIEvent(BUIEvent.SIZE_CHANGE));
            }
            super.draw();
            return;
        }// end function

        override public function drawNow() : void
        {
            super.drawNow();
            this.drawLayout();
            return;
        }// end function

        protected function drawLayout() : void
        {
            var _loc_1:DisplayObject = null;
            var _loc_2:Boolean = false;
            var _loc_3:Boolean = false;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            var _loc_7:Number = NaN;
            var _loc_8:Number = NaN;
            var _loc_9:Number = NaN;
            var _loc_10:Number = NaN;
            var _loc_11:LayoutInfo = null;
            var _loc_12:Number = NaN;
            var _loc_13:Number = NaN;
            var _loc_4:int = 0;
            while (_loc_4 < this.numChildren)
            {
                
                _loc_1 = getChildAt(_loc_4);
                _loc_2 = false;
                _loc_3 = false;
                if (_loc_1 is BUI)
                {
                    (_loc_1 as BUI).drawNow();
                }
                _loc_5 = _loc_1.width;
                _loc_6 = _loc_1.height;
                _loc_7 = Infinity;
                _loc_8 = Infinity;
                _loc_9 = -Infinity;
                _loc_10 = -Infinity;
                if (this._childsLayoutInfo[_loc_1])
                {
                    _loc_11 = this._childsLayoutInfo[_loc_1];
                    if (!isNaN(_loc_11.offsetCenterHorizontal))
                    {
                        _loc_1.x = this.width / 2 + _loc_11.offsetCenterHorizontal;
                        if (!isNaN(_loc_11.left))
                        {
                            _loc_1.width = this.width / 2 + _loc_11.offsetCenterH - _loc_11.left;
                        }
                        else if (!isNaN(_loc_11.right))
                        {
                            _loc_1.width = this.width / 2 + _loc_11.offsetCenterH - _loc_11.right;
                        }
                    }
                    else if (!isNaN(_loc_11.left) && !isNaN(_loc_11.right))
                    {
                        _loc_1.x = _loc_11.left;
                        _loc_1.width = this.width - _loc_11.left - _loc_11.right;
                        _loc_2 = true;
                    }
                    else if (!isNaN(_loc_11.left))
                    {
                        _loc_1.x = _loc_11.left;
                    }
                    else if (!isNaN(_loc_11.right))
                    {
                        _loc_1.x = this.width - _loc_11.right - _loc_5;
                    }
                    if (!isNaN(_loc_11.offsetCenterVertical))
                    {
                        _loc_1.y = (this.height - _loc_6) / 2 + _loc_11.offsetCenterVertical;
                    }
                    else if (!isNaN(_loc_11.top) && !isNaN(_loc_11.bottom))
                    {
                        _loc_1.y = _loc_11.top;
                        _loc_1.height = this.height - _loc_11.top - _loc_11.bottom;
                        _loc_3 = true;
                    }
                    else if (!isNaN(_loc_11.top))
                    {
                        _loc_1.y = _loc_11.top;
                    }
                    else if (!isNaN(_loc_11.bottom))
                    {
                        _loc_1.y = this.height - _loc_11.bottom - _loc_6;
                    }
                    if (!isNaN(_loc_11.widthPercent) && !_loc_2)
                    {
                        _loc_12 = this.width * _loc_11.widthPercent / 100;
                        if (_loc_12 + _loc_1.x > this.width)
                        {
                            _loc_12 = this.width - _loc_1.x;
                        }
                        _loc_1.width = _loc_12;
                    }
                    if (!isNaN(_loc_11.heightPercent) && !_loc_3)
                    {
                        _loc_13 = this.height * _loc_11.heightPercent / 100;
                        if (_loc_13 + _loc_1.y > this.height)
                        {
                            _loc_13 = this.height - _loc_1.y;
                        }
                        _loc_1.height = _loc_13;
                    }
                    if (_loc_1 is BUI)
                    {
                        (_loc_1 as BUI).drawNow();
                    }
                    _loc_7 = _loc_7 < _loc_1.x ? (_loc_7) : (_loc_1.x);
                    _loc_8 = _loc_8 < _loc_1.y ? (_loc_8) : (_loc_1.y);
                    _loc_9 = _loc_9 > _loc_1.width + _loc_1.x ? (_loc_9) : (_loc_1.width + _loc_1.x);
                    _loc_10 = _loc_10 > _loc_1.height + _loc_1.y ? (_loc_10) : (_loc_1.height + _loc_1.y);
                }
                _loc_4++;
            }
            return;
        }// end function

        public function get childsLayoutInfo() : Dictionary
        {
            return this._childsLayoutInfo;
        }// end function

    }
}
